<template>
  <div class="line-query-layout">
    <header class="query">
      <input type="text" class="query-inp" :placeholder="noticeVal" v-model="searchVal" autofocus @input="lineInp">
    </header>
    <ul class="list">
      <li class="list-item" v-for="(item,index) in searchList" @click="toDetail(item)">
        <i class="fa fa-clock-o clock" aria-hidden="true"></i>
        <span class="name">{{item.lineName}}路</span>
        <span class="start">（{{item.startStopName}}</span>
        <i class="fa fa-long-arrow-right arrow" aria-hidden="true"></i>
        <span class="end">{{item.endStopName}}）</span>
      </li>
      <li class="list-item" v-show="clearList" @click="clearHistory">清空搜索记录</li>
    </ul>
  </div>
</template>
<script>
  import {getLine, addCollect} from '../utils/http'
  import {setHistory, showToast} from '../utils/public'
  import MessageBox from '../components/MessageBox/index'

  export default {
    computed: {
      noticeVal() {
        return global.lineConfig.lineQuery.noticeVal
      }
    },
    data() {
      return {
        searchVal: '',
        searchList: [],
        clearList: false
      }
    },
    mounted() {
      this.onReady()
    },
    methods: {
      onReady() {
        this.searchList = JSON.parse(localStorage.getItem('historyList'))
        if (this.searchList && this.searchList.length > 0) {
          this.clearList = true
        } else {
          this.clearList = false
        }
      },
      lineInp() {
        if (this.searchVal === '') {
          this.onReady()
        } else {
          this.clearList = false
          getLine({
            val: this.searchVal,
            cb: (res) => {
              this.searchList = res
            }
          })
        }
      },
      toDetail(item) {
        setHistory(item)
        let query = this.$route.query
        if (query.to && query.to === 'myCollect') {
          addCollect({
            lineId: item.lineId,
            lineNo: item.lineNo,
            direction: item.direction,
            cb: () => {
              this.$router.push({path: 'myCollect'})
            }
          })
        } else if (query.to && query.to === 'lineSet') {
          localStorage.removeItem('lineSet')
          localStorage.setItem('lineDetail', JSON.stringify(item))
          localStorage.setItem('setLineId', JSON.stringify(item))
          this.$router.push({path: 'lineSet', query: {from: 'self'}})
        } else {
          localStorage.setItem('lineDetail', JSON.stringify(item))
          this.$router.push({path: '/lineDetail'})
        }
      },
      clearHistory() {
        MessageBox({
          type: 'confirm',
          content: '确定清空历史记录?',
          okVal: '确定',
          cancelVal: '取消',
          callback: (action) => {
            if (action === 'ok') {
              localStorage.removeItem('historyList')
              showToast({message: '清空成功', duration: 1000})
              this.onReady()
            }
          }
        })
      }
    }
  }
</script>
<style scoped>
  @import '../assets/css/lineQuery.css';
</style>
